/*******************************************************************************
	   Authors: Neil Himwich
	   Project: ETS
      Purpose: Make balance tables split by whether or not the firms reported more
               than the median level.

	   Date created: June 16 2023
	   Version:      STATA 17 MP

	   Last edited: June 16 2023
      Edited by: Neil
****************************************************************************/

set more off
clear matrix
clear all
pause on

use "$EMISSIONS_DATA_IN/CEMS_2018-12-31 to 2021-04-03_StackDay_Balanced_HistCFs_Master337s_3sample2trunc.dta", clear

* Need to get a measure on per industry level of reporting: Find average across all stacks per day and mean across days
* Need this as the baseline covariates are only at the industry level
bysort date gpcb_id: egen tot_report_hrs_id_day = mean(tot_report_hrs)
bysort date gpcb_id: keep if _n==1
bysort gpcb_id: egen mean_report_hrs_id = mean(tot_report_hrs_id_day)
bysort gpcb_id: keep if _n==1
keep gpcb_id mean_report_hrs_id

merge 1:1 gpcb_id using "$BASELINE_DATA_OUT/BaselineCovariates.dta"
** All matched
drop _merge
_pctile mean_report_hrs_id, p(50)
gen D_high_report = mean_report_hrs_id>`r(r1)'

do "$CODE_DIR/diff_test.do"

********************************************************************************
************************ [1] BALANCE CHECK (FULL SAMPLE) ***********************
********************************************************************************

gen D_small_scale = (size == 1)
replace D_small_scale = . if size == .
gen D_large_scale = (size == 3)
replace D_large_scale = . if size == .
replace boi_year = . if D_boi_year_missing == 1
replace bh_total_workers = . if D_bh_total_workers_missing == 1
replace bh_ope_cost_USD = . if D_bh_ope_cost_missing == 1
replace bh_total_capex_USD = . if D_bh_total_capex_missing == 1
replace grossrev_17_18_clean_USD = grossrev_17_18_clean_USD / 1000
replace bh_total_capex_USD = bh_total_capex_USD / 1000
replace bh_ope_cost_USD = bh_ope_cost_USD / 1000
replace elecbill_2017_clean_USD = elecbill_2017_clean_USD / 1000
gen above_standard = pm_conc_etsbl > 150
replace above_standard = . if pm_conc_etsbl == .


local cov_interest_A = "elecbill_2017_clean_USD ln_plant_total_heatoutput size D_small_scale D_large_scale pm_cnsnt_stcks D_textiles  grossrev_17_18_clean_USD"
local cov_interest_B = "bh_total_workers bh_total_capex_USD bh_ope_cost_USD D_cyc D_bf D_scr D_esp"
local cov_interest_C = "pm_mass_etsbl pm_conc_etsbl mean_ring_premock above_standard"

label var grossrev_17_18_clean_USD "Gross Sales Revenue in 2017 (1,000 USD)"
label var elecbill_2017_clean_USD "Total electricity cost (1,000 USD)"
label var ln_plant_total_heatoutput "Log(plant total heat output)"
label var size "Size as recorded on environment consent (1 to 3)"
label var D_small_scale "Small-scale (size=1)"
label var D_large_scale "Large-scale (size=3)"
label var pm_cnsnt_stcks "Number of stacks"
label var D_textiles "Textiles sector (=1)"
label var bh_total_workers "Boiler house employment"
label var bh_total_capex_USD "Boiler house capital expenditure (1,000 USD)"
label var bh_ope_cost_USD "Boiler house operating cost (1,000 USD)"
label var D_cyc "APCD: Cyclone present"
label var D_bf "APCD: Bag ﬁlter present"
label var D_scr "APCD: Scrubber present"
label var D_esp "APCD: ESP present"
label var pm_mass_etsbl "Plant total PM mass rate (kg/hr)"
label var pm_conc_etsbl "Plant mean PM concentration (mg/Nm\textsuperscript{3})"
label var mean_ring_premock "Plant mean Ringelmann score (1 to 5)"
label var above_standard "Above regulatory standard at ETS baseline (=1)"

di "Data for Table A2: Distribution of number of stacks by plant"
tab pm_cnsnt_stcks
tab pm_cnsnt_stcks if D_treatment == 1
tab pm_cnsnt_stcks if D_treatment == 0

********************************************************************************
********************** [2] BALANCE CHECK (ANALYSIS SAMPLE) *********************
********************************************************************************

merge 1:m gpcb_id using "$IMPUTATION_DATA_OUT/PlantMonthPMMassRule0.dta",
keep if _merge == 3
bysort industry_id: gen dump=_n
bysort industry_id: keep if _n==_N

diffTest3 `cov_interest_A', by(D_high_report)
esttab using "$EMISSIONS_TABS/Table_F3.tex", replace fragment ///
	noobs nomtitles nonumbers collabels("Over Median" "Under Median" "Difference") ///
	varwidth(32) label nogap compress booktabs width(0.75\hsize) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead( ///
		"\begin{longtable}{lccc}" ///
		"\caption{Balance of plant characteristics by whether report more than median reporting}" ///
		"\label{tab:balanceDA} \\" ///
		"\toprule") ///
	posthead( ///
	  "&(1)&(2)&(3)\\" ///
		"\midrule" ///
		"\addlinespace" ///
		"\multicolumn{4}{c}{\textit{Panel A: Plant Measures}} \\" ) ///
	postfoot("\addlinespace\addlinespace")

diffTest3 `cov_interest_B', by(D_high_report)
esttab using "$EMISSIONS_TABS/Table_F3.tex", append fragment ///
	noobs nomtitles nonumbers collabels(none) ///
	varwidth(32) label nogap booktabs width(0.75\hsize) eqlabel(none) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead("\multicolumn{4}{c}{\textit{Panel B: Plant Abatement and Investment Cost}} \\") ///
	posthead("") ///
	prefoot("") ///
	postfoot("\addlinespace\addlinespace")

diffTest3 `cov_interest_C', by(D_high_report)
esttab using "$EMISSIONS_TABS/Table_F3.tex", append fragment ///
	noobs nomtitles nonumbers collabels(none) ///
	varwidth(32) label  nogap booktabs width(0.75\hsize) eqlabel(none) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead("\multicolumn{4}{c}{\textit{Panel C: Plant Pollution Measures}} \\") ///
	posthead("") ///
	prefoot("\addlinespace\addlinespace") ///
	postfoot( ///
		"Number of plants & 156 & 136 & \\" ///
		"\bottomrule\\[-5.2ex]" ///
		"\insertTableNotes" ///
		"\end{longtable}" )
